<template>
  <div class="monitor">
    <div class="title">
      <p class="tip">监控事件单</p>
      <button @click="back" class="goBack">返回</button>
    </div>
    <div class="contain">
      <el-container>
        <el-header>
          <el-menu
            :default-active="activeIndex"
            class="el-menu-demo"
            mode="horizontal"
            @select="handleSelect"
          >
            <el-menu-item index="1">接手超时</el-menu-item>
            <el-menu-item index="2">更新超时</el-menu-item>
          </el-menu>
        </el-header>
        <el-main>
          <div class="main">
            <el-form ref="searchForm" :inline="true" class="demo-form-inline">
              <el-form-item label="系统名称">
                <!-- <el-autocomplete
                  clearable
                  class="inline-input"
                  v-model="state"
                  :fetch-suggestions="querySearch"
                  placeholder="请输入系统名称"
                  :trigger-on-focus="false"
                  @select="handleSelect_orgId"
                ></el-autocomplete> -->
                <el-select
                  v-model="state"
                  placeholder="请选择系统"
                  style="width: 240px"
                  filterable
                  clearable
                  @change="handleSelect_orgId"
                >
                  <el-option
                    v-for="(item, index) in restaurants"
                    :key="index + 1"
                    :label="item.value"
                    :value="item.orgId"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="单号" prop="changeCode">
                <el-input
                  clearable
                  v-model="changeCode"
                  class="inline-input"
                  placeholder="请输入单号"
                />
              </el-form-item>
              <el-form-item label="标题" prop="changeTitle">
                <el-input
                  clearable
                  v-model="changeTitle"
                  class="inline-input"
                  placeholder="请输入标题"
                />
              </el-form-item>
              <el-form-item label="所属中心" prop="center">
                <el-select v-model="center" placeholder="请选择中心">
                  <el-option label="合肥中心" value="0" />
                  <el-option label="丰台中心" value="2" />
                  <el-option label="亦庄中心" value="1" />
                  <el-option label="廊坊中心" value="3" />
                  <el-option label="无所属中心" value="5" />
                </el-select>
              </el-form-item>
              <el-form-item label="所属等级" prop="Event_level">
                <el-select v-model="Event_level" placeholder="请选择等级">
                  <el-option label="一般" value="1" />
                  <el-option label="急" value="2" />
                  <el-option label="紧急" value="3" />
                </el-select>
              </el-form-item>
            </el-form>

            <div class="button">
              <button @click="detail" class="btn">查看详情</button>
              <button @click="search" @keyup.enter="monitor()" class="btn reset"> 搜索 </button>
              <button @click="resetForm" class="btn">重置</button>
              <el-dropdown trigger="click">
                <span class="el-dropdown-link">
                  导出<i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item icon="el-icon-plus" @click="downloadExcel">
                      导出当前页</el-dropdown-item
                    >
                    <el-dropdown-item icon="el-icon-circle-plus" @click="Select_downloadExcel">
                      导出所有页</el-dropdown-item
                    >
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
          </div>
          <div class="tableBox">
            <div class="desc">
              <img src="../../assets/images/bbsm.png" alt="" />
              {{ desc }}
            </div>
            <div class="table" ref="container">
              <el-table
                :height="tableHeight"
                ref="multipleTableRef"
                :data="changeList"
                tooltip-effect="dark"
                @select="selectChange"
                @selection-change="handleSelectionChange"
                row-key="getRowKey"
                @row-click="handleRowClick"
              >
                <el-table-column
                  align="center"
                  :reserve-selection="false"
                  type="selection"
                ></el-table-column>
                <el-table-column
                  show-overflow-tooltip
                  align="center"
                  prop="eventNo"
                  label="单号"
                ></el-table-column>
                <!-- <el-table-column
                            show-overflow-tooltip
                            align="center"
                            prop="eventId"
                            label="告警Id"
                        >
            </el-table-column>-->
                <el-table-column
                  align="center"
                  prop="eventTitle"
                  label="事件标题"
                  show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                  align="center"
                  prop="createTime"
                  label="创建时间"
                  show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                  align="center"
                  prop="eventSource"
                  label="来源"
                  show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                  align="center"
                  prop="appSystemName"
                  label="系统名称"
                  show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                  align="center"
                  prop="affiliatedCenter"
                  label="所属中心"
                  show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                  align="center"
                  prop="eventLevel"
                  label="事件等级"
                  show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                  align="center"
                  prop="eventType"
                  label="事件类型"
                  show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                  align="center"
                  prop="closeTime"
                  label="告警关闭时间"
                  show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                  align="center"
                  prop="status"
                  label="事件状态"
                  show-overflow-tooltip
                ></el-table-column>
              </el-table>
            </div>

            <!-- 分页 -->
            <el-pagination
              ref="pagination"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pageNum"
              :page-sizes="[10, 15, 20]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
            ></el-pagination>
          </div>
        </el-main>
      </el-container>
    </div>
  </div>
</template>
<script setup lang="ts">
  import { ElMessage } from 'element-plus';
  import { changeOrganization } from '@/http/api';
  import { eventDeail, Countexport, download } from '@/http/api/monitor';
  import { ref, onMounted, onBeforeUnmount, reactive, watch } from 'vue';
  import { useRouter, useRoute } from 'vue-router';
  //   import { useStore } from 'vuex';
  //   const store = useStore();
  const router = useRouter();
  const route = useRoute();
  const changeList = reactive([]);
  const pageNum = ref(1);
  const pageSize = ref(10);
  const total = ref(0);
  const changeTitle = ref('');
  const changeCode = ref('');
  const Event_level = ref('');
  const center = ref('');
  const restaurants = reactive([]);
  const container = ref(null);
  const tableHeight = ref(0);
  const state = ref('');
  const orgId = ref('');
  const desc = ref('');
  const multipleTableRef = ref<InstanceType<typeof ElTable>>();
  const multipleTable = ref<User[]>([]);
  const eventId = ref(''); // 告警ID
  // const type = ref(route.params.type);
  //const value = ref('');
  const activeIndex = ref(route.params.type ? route.params.type : '1');

  onMounted(() => {
    if (activeIndex.value == '1') {
      desc.value = '接手超时：接手操作超过考核时限的未接手的监控事件单';
    } else {
      desc.value = '更新超时：更新操作超过考核时限的已接手的监控事件单';
    }
    getPage();
    enterKeyup();
    calHeight();
    changeOrgan();
    eventDeail_list();
  });
  const getPage = () => {
    const localPage = localStorage.getItem('monitorListPage');
    if (localPage != null) {
      pageNum.value = Number(localPage);
    } else {
      pageNum.value = 1;
    }
  };
  const handleSelect = (key: any) => {
    activeIndex.value = key;
    eventDeail_list();
  };
  const monitor = () => {
    document.onkeydown = (e) => {
      if (e.keyCode == 13) {
        search();
      }
    };
  };
  const enterKeyupDestroyed = () => {
    document.removeEventListener('keyup', monitor());
  };
  const enterKeyup = () => {
    document.addEventListener('keyup', monitor());
  };

  const calHeight = () => {
    const rect = container.value.getBoundingClientRect();
    tableHeight.value = rect.height;
  };
  const changeOrgan = () => {
    changeOrganization().then((res) => {
      if ((res as any).data.code !== 0) {
        ElMessage({
          message: (res as any).data.msg,
          type: 'error'
        });
        return;
      }
      restaurants.length = 0;
      res.data.data.forEach((item) => {
        (restaurants as any).push({
          value: item.orgName,
          orgId: item.orgId
        });
      });
    });
  };
  const eventDeail_list = () => {
    eventDeail({
      pageSize: pageSize.value,
      pageNum: pageNum.value,
      eventNo: changeCode.value,
      eventId: eventId.value,
      eventTitle: changeTitle.value,
      appSystemCode: orgId.value,
      adjectiveType: activeIndex.value,
      level: Event_level.value,
      center: center.value
    }).then((res) => {
      if ((res as any).data.code !== 0) {
        ElMessage({
          message: (res as any).data.msg,
          type: 'error'
        });
        return;
      }
      changeList.length = 0;
      (changeList as any).push(...res.data.data.rows);
      total.value = res.data.data.total;
    });
  };
  // 导出当前页
  const downloadExcel = () => {
    // Export2Excel(this.columns, this.changeList);
    if (changeList.length === 0) {
      this.$message({
        message: '当前暂无数据',
        type: 'warning'
      });
      ElMessage({
        message: '当前暂无数据',
        type: 'error'
      });
      return;
    }
    exportlist(changeList.value);
  };
  const exportlist = () => {
    Countexport({
      pageSize: pageSize.value,
      pageNum: pageNum.value,
      eventNo: changeCode.value,
      eventId: eventId.value,
      eventTitle: changeTitle.value,
      appSystemCode: orgId.value,
      adjectiveType: activeIndex.value,
      level: Event_level.value,
      center: center.value,
      currentPage: 'currentPage'
    }).then((res) => {
      if ((res as any).data.code !== 0) {
        ElMessage({
          message: (res as any).data.msg,
          type: 'error'
        });
        return;
      }
      download_list(res.data.msg);
    });
  };
  const download_list = (type) => {
    download({
      fileName: type,
      delete: true
    }).then((res) => {
      const link = document.createElement('a');
      let blob = new Blob([res.data], {
        type: 'application/vnd.ms-excel'
      });
      link.style.display = 'none';
      link.href = URL.createObjectURL(blob);
      link.download = type; //下载的文件名
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    });
  };
  // 导出所有的
  const Select_downloadExcel = () => {
    // this.export(this.changeListAll);
    Countexport({
      pageSize: pageSize.value,
      pageNum: pageNum.value,
      eventNo: changeCode.value,
      eventId: eventId.value,
      eventTitle: changeTitle.value,
      appSystemCode: orgId.value,
      adjectiveType: activeIndex.value,
      level: Event_level.value,
      center: center.value,
      currentPage: ''
    }).then((res) => {
      if ((res as any).data.code !== 0) {
        ElMessage({
          message: (res as any).data.msg,
          type: 'error'
        });
        return;
      }
      download_list(res.data.msg);
    });
  };

  // 重置
  const resetForm = () => {
    // this.$refs.searchForm.resetFields();
    orgId.value = '';
    state.value = '';
    changeCode.value = '';
    changeTitle.value = '';
    eventId.value = '';
    Event_level.value = '';
    center.value = '';
    // this.multipleTable = [];
  };
  const search = () => {
    if (!state.value) {
      orgId.value = '';
    } else {
      state.value = state.value.replace(/(^\s*)|(\s*$)/g, '');
      if (!orgId.value) {
        orgId.value = state.value;
      }
    }
    pageNum.value = 1;
    changeTitle.value = changeTitle.value.replace(/(^\s*)|(\s*$)/g, '');
    changeCode.value = changeCode.value.replace(/(^\s*)|(\s*$)/g, '');
    eventId.value = eventId.value.replace(/(^\s*)|(\s*$)/g, '');
    Event_level.value = Event_level.value.replace(/(^\s*)|(\s*$)/g, '');
    center.value = center.value.replace(/(^\s*)|(\s*$)/g, '');
    eventDeail_list();
  };
  // 分页
  const handleSizeChange = (size) => {
    pageSize.value = size;
    eventDeail_list();
  };
  const handleCurrentChange = (num) => {
    pageNum.value = num;
    localStorage.setItem('monitorListPage', num);
    eventDeail_list();
  };

  const back = () => {
    // this.$router.go(-1);
    router.push({
      name: 'restructure'
    });
  };

  // 点击行触发，选中或不选中复选框
  const handleRowClick = (row) => {
    multipleTable.value = row;
    multipleTableRef.value.toggleRowSelection(row);
    selectChange(multipleTable.value);
  };
  const selectChange = (selection: string) => {
    if (selection.length > 1) {
      const del_row = selection.shift();
      multipleTableRef.value!.toggleRowSelection(del_row);
    }
  };
  const handleSelectionChange = (val: string) => {
    multipleTable.value = val;
  };

  const detail = () => {
    if (!multipleTable.value.length) {
      Elessage({
        message: '请先勾选查看的数据',
        type: 'warning'
      });
    } else {
      router.push({
        name: 'monitor_detail',
        params: {
          value: activeIndex.value,
          type: multipleTable.value[0].eventId,
          level: Event_level.value,
          center: center.value
        }
      });
    }
  };
  // selectInfo(selection, row) {
  //     // 清除 所有勾选项
  //     this.$refs.multipleTable.clearSelection();
  //     // 当表格数据都没有被勾选的时候 就返回
  //     // 主要用于将当前勾选的表格状态清除
  //     if (selection.length == 0) return;
  //     this.$refs.multipleTable.toggleRowSelection(row, true);
  // },

  // 选中的系统对应ID
  const handleSelect_orgId = (item) => {
    orgId.value = item;
  };

  // // 系统名过滤
  // const querySearch = (queryString: string, cb) => {
  //   const results = queryString ? restaurants.filter(createFilter(queryString)) : restaurants;
  //   // 调用 callback 返回建议列表的数据

  //   cb(results);
  // };
  // const createFilter = (queryString: striing) => {
  //   return (restaurant) => {
  //     if (restaurant.value) {
  //       //return restaurant.value.includes(queryString);
  //       return restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0;
  //     }
  //   };
  // };
  onBeforeUnmount(() => {
    enterKeyupDestroyed();
  });
  watch(
    () => router.options.history.state.back,
    (newVal) => {
      if (newVal == '/restructure') {
        localStorage.removeItem('monitorListPage');
      }
    },
    { immediate: true }
  );
</script>
<style lang="less" scoped>
  .monitor {
    width: 100%;
    height: 100vh;
    background: #eaeef4;
    padding: 20px;
    box-sizing: border-box;
    //background-image: url("../../assets/images/background-image.jpg");

    .title {
      display: flex;
      justify-content: space-between;
      margin-bottom: 15px;
      .tip {
        height: 34px;
        line-height: 34px;
        font-size: 18px;
        font-weight: 400;
        color: #46545d;
        margin: 0;
        padding: 0;
      }
      .goBack {
        background: #007af5;
        width: 80px;
        height: 34px;
        border: none;
        color: #fff;
        font-size: 12px;
        font-weight: 400;
      }
    }
    .tabs_item {
      height: calc(100% - 50px);
      background: #fff;
      padding: 20px;
      box-sizing: border-box;
      :deep(.el-tabs) {
        height: 100%;
        .el-tabs__header {
          .el-tabs__nav-wrap::after {
            width: 0;
          }
          .el-tabs__nav-wrap {
            .el-tabs__nav-scroll {
              .el-tabs__nav {
                .el-tabs__active-bar {
                  height: 2px;
                }
                .el-tabs__item {
                  font-size: 16px;
                  font-weight: 400;
                  color: #46545d;
                  &.is-active {
                    font-weight: bold;
                    color: #007af5;
                  }
                }
              }
            }
          }
        }
        .el-tabs__content {
          height: calc(100% - 40px);
          .el-tab-pane {
            height: 100%;
          }
        }
      }
    }
    .contain {
      background: #fff;
      height: calc(100% - 50px);
      .el-container {
        height: 100%;
        .el-header {
          .el-menu--horizontal {
            border-bottom: none;
            .el-menu-item {
              font-size: 16px;
              font-weight: 400;
              color: #46545d;
              &.is-active {
                font-size: 16px;
                font-weight: bold;
                color: #007af5;
                border-bottom: 2px solid #007af5;
              }
            }
          }
        }
        .el-main {
          .main {
            display: flex;
            justify-content: space-between;
            margin-bottom: 15px;
            .demo-form-inline {
              display: flex;
              height: 34px;
              max-width: 70%;
              width: 70%;
              //align-items: center;
              :deep(input::placeholder) {
                font-size: 14px;
                font-weight: 400;
                color: #bbbbbb;
              }
              :deep(.el-form-item) {
                margin-bottom: 0;
                line-height: 34px;
                height: 34px;
                width: 40%;
              }
              :deep(.el-form-item__content) {
                line-height: 34px;
                //height: 43px;
                width: 80%;
                :deep(.inline-input) {
                  // width: 230px;
                  height: 34px;
                }
              }
              :deep(.el-input__inner) {
                // width: 200px;
                // height: 34px;
                background: #ffffff;
                // border: 1px solid #dddddd;
                border-radius: 2px;
              }
              :deep(.el-form-item__label) {
                // color: #6fd7f7;
                font-size: 14px;
                font-weight: 400;
                color: #46545d;
                height: 34px;
                line-height: 34px;
              }
            }
            .button {
              display: flex;
              justify-content: space-around;
              max-width: 40%;
              .btn {
                width: 80px;
                height: 34px;
                line-height: 34px;
                background: #007af5;
                border-radius: 2px;
                font-size: 12px;
                font-weight: 400;
                color: #ffffff;
                border: 0;
                cursor: pointer;
                &.reset {
                  margin: 0 10px;
                }
              }
              #goBack {
                max-width: 131px;
                height: 34px;
                background: #007af5;
                border-radius: 2px;
                border: 0;
                font-size: 12px;
                font-weight: 400;
                color: #ffffff;
                cursor: pointer;
              }
              .el-dropdown {
                margin: 0px 10px;
                cursor: pointer;
              }
              .el-dropdown-link {
                // display: inline-block;
                width: 80px;
                height: 34px;
                line-height: 34px;
                text-align: center;
                color: #fff;
                background: #007af5;
                border-radius: 2px;
              }
            }
          }

          .tableBox {
            height: calc(100% - 50px);
            background: #fff;
            // padding: 20px;
            box-sizing: border-box;
            //overflow: hidden;
            .desc {
              font-size: 12px;
              font-weight: 400;
              color: #ec4b5a;
              background: #fbdbde;
              line-height: 33px;
              height: 33px;
              text-align: center;
              border-radius: 2px;
              display: flex;
              align-items: center;
              justify-content: center;

              img {
                width: 16px;
                height: 16px;
                // margin: auto;
                margin-right: 10px;
              }
            }
            .table {
              // background: rgba(0, 0, 0, 0.1);
              height: calc(100% - 93px);
              width: 100%;
              margin-bottom: 15px;
              :deep(.el-table-column--selection.is-leaf .el-checkbox) {
                display: none;
              }
              :deep(.el-table__cell.is-leaf) {
                //background: rgba(0, 0, 0, 0.1);
                // color: #6fd7f7;
                //color: #fff;
                color: #929ea8;
              }

              :deep(tr:hover > td) {
                background-color: transparent;
              }
              :deep(.el-table__empty-text) {
                //color: #fff;
                color: #929ea8;
              }
              :deep(td.el-table__cell) {
                //color: #fff;
                color: #929ea8;
              }
            }
            .el-pagination {
              height: 34px;
              padding: 0;
              box-sizing: border-box;
              //width: 50%;
              :deep(.el-pagination__total) {
                font-size: 14px;
                font-weight: 400;
                color: #bbbbbb;
                line-height: 34px;
              }

              :deep(.el-input__inner) {
                height: 34px;
                //width: 60px;
                background: #fff;
                //color: #66b1ff;
                color: #46545d;
              }
              :deep(button:disabled) {
                background: #fff;
                height: 34px;
                line-height: 34px;
                border: 1px solid #dddddd;
                //color: #66b1ff;
                color: #46545d;
                box-sizing: border-box;
              }
              :deep(.el-pager li) {
                //background: rgba(0, 0, 0, 0.1);
                background: #fff;
                height: 34px;
                line-height: 34px;
                border: 1px solid #dddddd;
                //color: #66b1ff;
                color: #46545d;
                box-sizing: border-box;
                &.active {
                  // color: #fff;
                  color: #46545d;
                }
              }
              :deep(.btn-prev) {
                background: #fff;
                height: 34px;
                line-height: 34px;
                border: 1px solid #dddddd;
                //color: #66b1ff;
                color: #46545d;
                box-sizing: border-box;
              }
              :deep(.btn-next) {
                background: #fff;
                height: 34px;
                line-height: 34px;
                border: 1px solid #dddddd;
                //color: #66b1ff;
                color: #46545d;
                box-sizing: border-box;
              }
              :deep(span:not([class*='suffix'])) {
                color: #bbbbbb;
              }
              .el-pagination__jump {
                height: 34px;
                line-height: 34px;
              }
            }
          }
        }
      }
    }
  }
</style>
